JSON example with RESTEasy + JAXB + Jettison
RESTEasy uses Jettison JSON library to map JAXB annotation object to and from JSON. In this tutorial, we show you how to convert an JAXB annotated object into JSON format and return it back to client.
Jackson as JSON provider
You may also interest to read this RESTEasy + Jackson example.
You may also interest to read this RESTEasy + Jackson example.
1. RESTEasy + JAXB + Jettison
To use JSON in RESTEasy, you need following dependencies.
File : pom.xml
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.2.0.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.2.0.GA</version>
</dependency>
</dependencies>
2. JAXB XML Provider
Create an object, annotate with JAXB. Why using XML provider? No worry, later you will use @BadgerFish
to convert it into JSON format.
package com.mkyong.rest;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "movie")
public class Movie {
String name;
String director;
int year;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
@XmlAttribute
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
3. JAX-RS
To return a JSON file format, annotate the service method with @BadgerFish
and @Produces("application/json")
.
RESTEasy will handle the JSON conversion automatically.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
@Path("/json/movie")
public class JSONService {
@BadgerFish
@GET
@Path("/get")
@Produces("application/json")
public Movie getMovieInJSON() {
Movie movie = new Movie();
movie.setName("Transformers: Dark of the Moon");
movie.setDirector("Michael Bay");
movie.setYear(2011);
return movie;
}
}
4. Demo
When URI pattern “/json/movie/get” is requested, following JSON file will be returned.
{
"movie":
{
"@year":"2011",
"director":{
"$":"Michael Bay"
},
"name":{
"$":"Transformers: Dark of the Moon"
}
}
}
Download Source Code
Download it – JAX-RS-Download-JSON-JAXB-Example.zip (7 KB)
How can i send JSON request to JAXB
How to remove $ and @ JASON keys from response
I have faces following issue in my jboss7.1.1Final and jboss 7.0.2 Give me suggestion to fix this issue
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/RESTfulDemoApplication]] (MSC service thread 1-6) Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext from [Module “org.scannotation.scannotation:main” from local module loader @88b0b3 (roots: E:jboss-as-7.0.2.Finalmodules)]
your application throws an exception when I run with jboss 7.1.1
Exception:
You have solution for this?
Would like to see how you consume the JSON object.
Hi,
How do you filter attribute you want to produce ?
in your example, what can i do if i don’t want the Director attribute ?
and please, don t tell me to build dto !
thx
i precise : in json format